package cn.kgc.mushroomdatabaseprovider.service.serviceimpl;

import cn.kgc.mushroomcommon.pojo.*;
import cn.kgc.mushroomdatabaseprovider.mapper.*;
import cn.kgc.mushroomdatabaseprovider.service.CommodityService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
@Transactional(readOnly = true)
public class CommodityServiceImpl implements CommodityService {

    @Resource
    CommodityMapper commodityMapper;

    @Override
    public List<Commodity> selectAll() {
        List<Commodity> commodities = commodityMapper.selectAll();
        return commodities;
    }

    @Override
    public List<Commodity> selectTopsan() {
        List<Commodity> commodities = commodityMapper.selectTopsan();
        return commodities;
    }

    @Override
    public Commodity selectCommodityAndDetail(Integer id) {
        return commodityMapper.selectCommodityAndDetail(id);
    }

    @Override
    public List<Commodity> selectByTypeId(Integer tidcommtype) {
        List<Commodity> commodities = commodityMapper.selectByTypeId(tidcommtype);
        return commodities;
    }

    @Resource
    TypeMapper typeMapper;

    @Resource
    CommtypeMapper commtypeMapper;

    @Override
    public List<Commodity> selectCommodityByTitleOrTypeName(String commOrTypeName,
                                                            Integer tiaojian,
                                                            String smallMoney,
                                                            String bigMoney) {
        List<Commodity> commodityList = new ArrayList<>();
        if (commOrTypeName != null) {
            //根据标题查询并添加到list集合里
            CommodityExample commodityExample = new CommodityExample();
            CommodityExample.Criteria commodityExampleCriteria = commodityExample.createCriteria();
            commodityExampleCriteria.andCommnameLike("%" + commOrTypeName + "%");
            if(smallMoney != null && bigMoney != null && Double.parseDouble(smallMoney)<Double.parseDouble(bigMoney)){
                commodityExampleCriteria.andPriceBetween(Double.parseDouble(smallMoney),Double.parseDouble(bigMoney));
            }
            List<Commodity> commodities = commodityMapper.selectByExample(commodityExample);
            commodityList.addAll(commodities);
            //根据类型查询id
            TypeExample typeExample = new TypeExample();
            TypeExample.Criteria typeExampleCriteria = typeExample.createCriteria();
            typeExampleCriteria.andTypenameLike("%" + commOrTypeName + "%");
            List<Type> types = typeMapper.selectByExample(typeExample);
            for (Type type : types) {
                //根据id查询中间表
                CommtypeExample commtypeExample = new CommtypeExample();
                CommtypeExample.Criteria commtypeExampleCriteria = commtypeExample.createCriteria();
                commtypeExampleCriteria.andTidcommtypeEqualTo(type.getId());
                List<Commtype> commtypes = commtypeMapper.selectByExample(commtypeExample);
                for (Commtype commtype : commtypes) {
                    //根据中间表shopid查询shop并添加到list集合里
                    CommodityExample commodityExample1 = new CommodityExample();
                    CommodityExample.Criteria commodityExampleCriteria1 = commodityExample1.createCriteria();
                    commodityExampleCriteria1.andIdEqualTo(commtype.getCidcommtype());
                    if(smallMoney != null && bigMoney != null && Double.parseDouble(smallMoney)<Double.parseDouble(bigMoney)){
                        commodityExampleCriteria1.andPriceBetween(Double.parseDouble(smallMoney),Double.parseDouble(bigMoney));
                    }
                    List<Commodity> commodities1 = commodityMapper.selectByExample(commodityExample1);
                    if(commodities1 != null){
                        commodityList.addAll(commodities1);
                    }
                }
            }
        }
        if(tiaojian == 1){
            for (int i = 0; i < commodityList.size() - 1; i++) {
                for (int j = 1; j < commodityList.size() - i; j++) {
                    Commodity a;
                    if ((commodityList.get(j - 1).getPrice()).compareTo(commodityList.get(j).getPrice()) > 0) { // 比较两个整数的大小
                        a = commodityList.get(j - 1);
                        commodityList.set((j - 1), commodityList.get(j));
                        commodityList.set(j, a);
                    }
                }
            }
        }else if(tiaojian == 2){

        }
        return commodityList;
    }

    @Override
    public List<Commodity> selectShopAllByName(Integer tid) {
         List<Commodity> commodities = commodityMapper.selectShopAllByName(tid);
        return commodities;
    }
}